﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERP_RORZE_SYSTEM;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_AssWork : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public string work_id;

        public frm_Edit_AssWork()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.work_id = null;

            LoadGroup();

            this.workID.LostFocus += workID_LostFocus;
        }

        void workID_LostFocus(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.workID.Text.Trim()))
            {
                string id = this.workID.Text.Trim().ToUpper();
                if (id.Length >= 3)
                {
                    id = id.Substring(id.Length - 3);
                    try
                    {
                        myConnection.Open();
                        SqlCommand cmd_sql = myConnection.CreateCommand();
                        cmd_sql.CommandText = "SELECT TimeWorkName FROM [231_TimeWork]"
                            + " WHERE RIGHT(TimeWorkID, 3) = '" + id + "' GROUP BY TimeWorkName";
                        SqlDataReader dataReader = cmd_sql.ExecuteReader();
                        if (dataReader.Read())
                        {
                            this.workName.Text = dataReader["TimeWorkName"].ToString();                           
                        }
                        dataReader.Close();
                        cmd_sql.Dispose();
                        myConnection.Close();
                    }
                    catch
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }                        
                    }
                }                
            }
        }

        //////////////////////////////////////////
        /// Load Data
        /////////////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.work_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [231_TimeWork] WHERE TimeWorkID = '" + this.work_id + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.workID.Text = this.work_id;
                        this.workName.Text = dataReader["TimeWorkName"].ToString().Trim();
                        this.typeVN.Text = dataReader["TypeVN"].ToString().Trim();
                        this.typeEN.Text = dataReader["TypeEN"].ToString().Trim();
                        if (!string.IsNullOrEmpty(dataReader["Indirect"].ToString().Trim()))
                        {
                            this.indirectCheck.Checked = true;
                        }

                        this.remarkText.Text = dataReader["Remark"].ToString().Trim();

                        for (int i = 0; i < this.groupWork.Properties.Items.Count; i++)
                        {
                            ComboboxItem group = (ComboboxItem)this.groupWork.Properties.Items[i];
                            if (dataReader["TimeWorkGroup"].ToString().Trim() == group.Value.ToString())
                            {
                                this.groupWork.SelectedIndex = i;
                            }
                        }
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }
            return ret;
        }

        ////////////////////////////////////////////
        /// Load Currency List
        ////////////////////////////////////////////
        private void LoadGroup()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT TimeWorkGroup FROM [231_TimeWork] GROUP BY TimeWorkGroup ORDER BY TimeWorkGroup";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["TimeWorkGroup"].ToString();
                    item.Value = dataReader["TimeWorkGroup"].ToString();
                    this.groupWork.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get group list");
            }
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;            
            string work_name = null;
            string type_vn = null;            
            string type_en = null;
            string indirect = null;
            string work_group = null;
            string remark = null;
            
            ComboboxItem item = new ComboboxItem();
            
            if (string.IsNullOrEmpty((this.workID.Text.Trim())))
            {
                error = "Please enter Work ID";
            }
            else
            {
                this.work_id = this.workID.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.workName.Text.Trim()))
                {
                    error = "Please enter Work Name";
                }
                else
                {
                    work_name = this.workName.Text.Trim();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                type_vn = this.typeVN.Text.Trim();
                type_en = this.typeEN.Text.Trim();
                if (this.indirectCheck.Checked)
                {
                    indirect = "Indirect";
                }
                else
                {
                    indirect = null;
                }
                work_group = this.groupWork.Text;
                remark = this.remarkText.Text;
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [231_TimeWork] SET TimeWorkID = '" + this.work_id + "',"
                        + " TimeWorkName = N'" + work_name + "', TypeVN = N'" + type_vn + "', TypeEN = N'" + type_en + "',"
                        + " Indirect = '" + indirect + "', Remark = N'" + remark + "', TimeWorkGroup = '" + work_group + "'"
                        + " WHERE TimeWorkID = '" + this.work_id + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                                        
                    MessageBox.Show("Work updated success.");
                    this.Dispose();
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot update Assembly Work. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
